/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			idrawfile.cc
  \date			Jun 2012
  \author		TNick

  \brief		Contains the implementation of IDrawFile class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<QStringList>
#include	<QTemporaryFile>

#include	<usermsg.h>
#include	<cpg/cpglibrary.h>

#include	<cpg/file-formats/formatmanager.h>
//#include	<file-formats/build-in/bindrawfile.h>
//#include	<file-formats/build-in/sqldrawfile.h>
//#include	<file-formats/build-in/xmldrawfile.h>

#include	"idrawfile.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
IDrawFile::IDrawFile	( void )
	: MemTrack()
{
	FormatManager::append( this );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
IDrawFile::~IDrawFile	( void )
{
	FormatManager::removeOne( this );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool						IDrawFile::readToBA			(
		const QString & s_file, BBlocks::ByteBuffer & out, BBlocks::UserMsg & um )
{
	QString	s_e;
	/* open file */
	QFile file( s_file );
	if ( !file.open( QFile::ReadOnly ) )
	{
		s_e = QObject::tr( "Cannot read file %1:\n%2." )
				.arg( s_file )
				.arg( file.errorString() );
		um.add( BBlocks::UserMsgMgr::MSG_ERROR, s_e );
		return false;
	}

	bool b = out.reinitFromFile( file );

	if ( b == false )
	{
		um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
					"Failed to write file:\n"
					"%1\n"
					"The disk may be full.\n" )
				.arg( s_file )
				);
	}
	return b;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool						IDrawFile::writeFromBA			(
		const QString & s_file, BBlocks::ByteBuffer & cont, BBlocks::UserMsg & um )
{
	QString	s_e;
	/* open file */
	QFile file( s_file );
	if ( !file.open( QFile::WriteOnly ) )
	{
		s_e = QObject::tr( "Cannot write file %1:\n%2." )
				.arg( s_file )
				.arg(file.errorString() );
		um.add( BBlocks::UserMsgMgr::MSG_ERROR, s_e );
		return false;
	}

	/* do write */
	bool b = cont.saveToFile( file );

	if ( b == false )
	{
		um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
					"Failed to read file:\n"
					"%1\n"
					"Memory may have been exhausted.\n" )
				.arg( s_file )
				);
	}
	return b;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QTemporaryFile *			IDrawFile::getTempOpaq			(
		 const QString & s_file_base, BBlocks::UserMsg & um )
{
	QString s_tp = CpgLibrary::fileTagsTempDir();
	QString s_base = CpgLibrary::lastPathComp( s_file_base ) +
			"-" + name() + "-XXXXXX.tmp";
	QTemporaryFile * ret = new QTemporaryFile(
				s_tp + s_base
				);

	if ( ret->open() == false )
	{
		ret->setFileName( s_tp + "opaq-XXXXXX.tmp");
		if ( ret->open() == false )
		{
			um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
						"Unable to create temporary file in\n"
						"%1\n"
						"for file\n"
						"%2\n"
						"The full path that was attempted:\n"
						"%3\n")
					.arg( s_tp )
					.arg( s_file_base )
					.arg( s_tp + s_base )
					);

			delete ret;
			return NULL;
		}
	}
	return ret;
}
/* ========================================================================= */



/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
